<?php

//////////////////// PRIVATE KERNEL FOR G2 (CARRIER ATTACK) ////////////////////
function UpdateShipsForPilot($pilot) {

	$allsorties_r = mysql_query("
    	select
    		id
        from
        	sorties
        where
        	pilotid='$pilot'
            	and
        	eTank>0
    ");

    while ($sortie=mysql_fetch_assoc($allsorties_r)) {

    	$sort = $sortie[id];

        mysql_query("update sorties set cv='0', bb='0', ca='0', dd='0', ss='0' where id='$sort'");

        UpdateShipsForSortie($pilot,$sort);

    }

}

function Recalculate_Pilot_Rank ($pilot) {

   $q = mysql_query("
	Select
    	s.id,
		s.eAir as air,
		s.eSAir+s.eTank+s.eCar+s.eArt+s.eAaa+s.eWag+eShip as ground,
        s.SortieEND as end
 	from
    	sorties as s
    where
	    s.pilotid='$pilot'
    		and
        (
    		(UNIX_TIMESTAMP(s.endtime)-UNIX_TIMESTAMP(s.starttime))>=60*15
    			or
		    (s.eSAir+s.eTank+s.eCar+s.eArt+s.eAaa+s.eWag+s.eShip)>0
        		or
	        s.eAir>0
				or
            SortieEND!='TKON'
    	)
        	and
        SortieEND!='MIA'

    order by s.id");

echo mysql_error();

    $rank = 0;
    $trank = 0;

    $tN = 0;
    $tsumair = 0;
    $tsumground = 0;

    $N = 0;
    $sumair = 0;
    $sumground = 0;

    while ($sort = mysql_fetch_assoc($q)){

    	echo "=Sortie:".$sort[id]."\r\n";

    	//esli vilet boevoi, uspesho zavershen - beretsa v uchet
    	if ($sort[end] == 'TKON' or $sort[end] == 'CRASHED') {

        	$tN++;
            $tsumair += $sort[air];
            $tsumground += $sort[ground];

            if ($tN>=80 and ($tsumair >=70  or $tsumground>=150)) {
            	if ($rank<9)
        	    	$rank = 9;
            }
            elseif ($tN >=60 and ($tsumair >=50 or $tsumground>=120)) {
            	if ($rank<8)
    	        	$rank = 8;
            }
            elseif ($tN >=40 and ($tsumair >=35 or $tsumground>=100)) {
            	if ($rank<7)
	            	$rank = 7;
            }
            elseif ($tN >=30 and ($tsumair >=25 or $tsumground>=70)) {
            	if ($rank<6)
            		$rank = 6;
            }
            elseif ($tN >=20 and ($tsumair >=20 or $tsumground>=50)) {
            	if ($rank<5)
            		$rank = 5;
            }
            elseif ($tN >=15 and ($tsumair >=10 or $tsumground>=30)) {
            	if ($rank<4)
        	    	$rank = 4;
            }
            elseif ($tN >=10 and ($tsumair >=5 or $tsumground>=20)) {
            	if ($rank<3)
    	        	$rank = 3;
            }
            elseif ($tN >=5 and ($tsumair >=3 or $tsumground>=10)) {
            	if ($rank<2)
	            	$rank = 2;
            }
            elseif ($tN >=3 and ($tsumair >=1 or $tsumground>=5)) {
            	if ($rank<1)
	            	$rank = 1;
            }

        	echo "=war: N='$tN', tsumair='$tsumair',tsumground='$tsumground', rank='$rank' \r\n";

        }
        else {
			if ($sort[end] == 'DIED' or $sort[end] == 'CAPTURED') {
            	echo "=war, DIED.";
	        	$tN = 0;
    	        $tsumair = 0;
        	    $tsumground = 0;
            }
            echo "=NOT war";
        }
	}

	return $rank;

}


function Recalculate_Pilot_Awards ($pilot) {

	$awd_r = mysql_query("
    	select
	    	award1,
    		award2,
    		award3,
	    	award4,
    		award5,
    		award6,
	    	award7,
    		award8,
    		award9
		from
        	finalpilots
        where
        	pilotid='$pilot'
        ");

    $awd_f = mysql_fetch_assoc($awd_r);
    $awd[1] = $awd_f[award1];
    $awd[2] = $awd_f[award2];
    $awd[3] = $awd_f[award3];
    $awd[4] = $awd_f[award4];
    $awd[5] = $awd_f[award5];
    $awd[6] = $awd_f[award6];
    $awd[7] = $awd_f[award7];
    $awd[8] = $awd_f[award8];
    $awd[9] = $awd_f[award9];

	$last_sid_r = mysql_query("
    select
    	max(s.id) as id
    from
    	sorties as s
    where
    	SortieEND<>'TKON'
        	and
        SortieEND<>'CRASHED'
           	and
        SortieEND<>'BAIL'
        	and
        SortieEND<>'MIA'
        	and
    	pilotid='$pilot'
    ");
echo mysql_error();

    $last_sid = mysql_fetch_assoc($last_sid_r);

    $last_sid = $last_sid[id];

	$stat_r = mysql_query("
    select
    	army,
    	N,
     	bomberkills,
    	tankkills,
        kills,
        streak,
        gstreak,
        gkills,
	    shipkills,
    	ks,
        artillerykills,
        gs,

        maxsgkills
    from
    	finalpilots
    where
    	pilotid='$pilot'
    ");
echo mysql_error();
    $stat = mysql_fetch_assoc($stat_r);

    //////////////// CALC ROUTINE /////////////////////

    $ses = mysql_query("
    	Select
			eAir as allair,
            eAir-eBom as air,
            eBom as bom,
			eSAir + eTank + eCar + eArt + eAaa + eWag + eShip as ground,
    		eShip as ship,
            eArt as art,
            eTank as tank,
            wound,
            SortieEND as end
        from
           	sorties as s
        where
			pilotid=$pilot
            	and
            id > '$last_sid'
            	and
    		((UNIX_TIMESTAMP(s.endtime)-UNIX_TIMESTAMP(s.starttime))>=60*15
    			or
		    (s.eSAir+s.eTank+s.eCar+s.eArt+s.eAaa+s.eWag+s.eShip)>0
        		or
	        s.eAir>0)
            	and
	    	(SortieEND = 'TKON'
    	    	or
        	SortieEND = 'CRASHED')
        	");
echo mysql_error();

	if ($stat[N]) {
		$stat[ks] = $stat[streak] / $stat[N];
		$stat[gs] = $stat[gstreak] / $stat[N];
    	echo "ks, gs devision:
        		$stat[streak] / $stat[N] = $stat[ks];
				$stat[gstreak] / $stat[N] = $stat[gs];
                 ";
    }
    else {
		$stat[ks] = 0;
		$stat[gs] = 0;
	}

echo var_dump($stat);

	if ($stat[army] == 1) {

	     // DYNAMIC GET TYPE AWARDS
    	while ($sort = mysql_fetch_assoc($ses)) {

    		// awd1
	    	if ($sort[gkills]>=15 and $sort[end]=='TKON')
        		$awd[1] = 1;

    		// awd2
	    	if (($sort[allair]>=1 and $sort[wound])
        			or
    			($sort[wound] and $sort[ground] >= 15 and $stat[gs]>=4)
        	   )
	        	$awd[2] = 1;
    	    // awd3
        	if (($sort[allair]>=2 and $sort[wound])
        				or
    			($sort[wound] and $sort[tank] >= 8 and $stat[gs]>=8)
	           )
    	    	$awd[3] = 1;
    	    // awd4
        	if ($sort[allair]>=5)
    	    	$awd[4] = 1;
    	    // awd5
        	if (($sort[bom]>=3) or ($sort[tank]>=8))
    	    	$awd[5] = 1;
    	    // awd6
        	if (($sort[allair]>=5) and ($sort[wound]>=10) and  ($sort[end]=='TKON'))
				$awd[6] = 1;
		}

	    // Common GET TYPE AWARDS
    	// awd1
	    if ($stat[N]>= 1
    			and (
        	    	$stat[bomberkills]>=1
       				or
            		($stat[kills]>=3 and $stat[ks]>=1)
	                or
    	        	$stat[tankkills]>=3
        	        or
            		$stat[artillerykills]>=4
                	or
	            	$stat[shipkills]>=1
       			)
	        )
			$awd[1] = 1;
	    // awd2
		if ($stat[N]>= 5 and $stat[ks] >= 1)
	    	$awd[2] = 1;
    	// awd3
	    if (($stat[N]>= 10 and $stat[ks] >= 1) or ($stat[gkills]>= 20 and $stat[gks] >= 15))
    		$awd[3] = 1;
	    // awd4
    	if (($stat[N]>= 10 and $stat[ks] >= 1.2)
    					or
			($stat[N]>= 20 and $stat[ks] >= 0.6)
    	    			or
			($stat[N]>= 10 and $stat[tankkills] / $stat[N] >= 4)
	        			or
			($stat[N]>= 20 and $stat[gks] >= 20)
	       )
    	    $awd[4] = 1;
	    // awd5

	    // awd6
    	if (($stat[kills]>= 15 and $stat[ks] >= 1.5)
    					or
			($stat[kills]>= 20 and $stat[ks] >= 1)
    	    			or
			($stat[kills]>= 30)
        				or
        	($stat[gkills]>= 100)
	        			or
        	($stat[tankkills]>= 50)
	        			or
    	    ($stat[N]>= 20 and $stat[gkills] >= 80)
    	   )
    		$awd[6] = 1;
	    // awd7
    	if (($stat[kills]>= 25 and $stat[ks] >= 1.5)
    					or
			($stat[kills]>= 35 and $stat[ks] >= 1)
    	    			or
			($stat[kills]>= 50)
	        			or
    	    ($stat[gkills]>= 200)
        				or
        	($stat[tankkills]>= 100)
	        			or
        	($stat[N]>= 40 and $stat[gkills] >= 100)
	       )
    		$awd[7] = 1;
	    // awd8
    	if (($stat[kills]>= 50 and $stat[ks] >= 1.5)
    					or
			($stat[kills]>= 60 and $stat[ks] >= 1)
    	    			or
			($stat[kills]>= 80)
	        			or
    	    ($stat[gkills]>= 400)
        				or
        	($stat[tankkills]>= 200)
	        			or
        	($stat[N]>= 100 and $stat[gkills] >= 300)
	       )
    		$awd[8] = 1;
	    // awd9
    	if (($stat[kills]>= 70 and $stat[ks] >= 1.5)
    					or
			($stat[kills]>= 80 and $stat[ks] >= 1)
    	    			or
			($stat[kills]>= 100)
	        			or
    	    ($stat[gkills]>= 600)
        				or
        	($stat[tankkills]>= 300)
	        			or
        	($stat[N]>= 150 and $stat[gkills] >= 400)
	       )
    		$awd[9] = 1;
	}
    elseif ($stat[army] == 2) {

	     // DYNAMIC GET TYPE AWARDS
    	while ($sort = mysql_fetch_assoc($ses)) {

    		// awd1
	    	if ($sort[gkills]>=15 and $sort[end]=='TKON')
        		$awd[1] = 1;

    		// awd2
	    	if ($sort[ground] >= 25 and $sort[end]=='TKON')
				$awd[2] = 1;
    	    // awd3
        	if (($sort[wound] and $sort[allair]>=1)
        				or
			   ($sort[wound] and $sort[tank]>=8 and $sort[gks]>=8)
                        or
    			($sort[wound] and $sort[ground] >=30 and $stat[gs]>=10)
	           )
    	    	$awd[3] = 1;
		}

	    // Common GET TYPE AWARDS
    	// awd1
	    if ($stat[N]>= 1
    			and (
        	    	$stat[bomberkills]>=1
       				or
            		($stat[kills]>=3 and $stat[ks]>=1)
	                or
    	        	$stat[tankkills]>=3
        	        or
            		$stat[artillerykills]>=4
                	or
	            	$stat[shipkills]>=1
       			)
	        )
			$awd[1] = 1;
	    // awd2
		if ($stat[bomberkills]>=2
        			or
           			($stat[N]>= 5 and $stat[ks] >= 1)
                    or
    	        	$stat[tankkills]>=5
        	        or
            		$stat[artillerykills]>=10
                	or
	            	$stat[shipkills]>=2
                    )
	    	$awd[2] = 1;
    	// awd3
	    if ($stat[N]>= 10 and $stat[ks] >= 1.2)
    		$awd[3] = 1;
	    // awd4
    	if (($stat[N]>= 10 and $stat[ks] >= 1.2)
    					or
			($stat[N]>= 30 and $stat[ks] >= 0.3)
    	    			or
			($stat[N]>= 15 and $stat[tankkills] / $stat[N] >= 4)
	        			or
			($stat[N]>= 30 and $stat[gs] >= 10)
	       )
    	    $awd[4] = 1;
	    // awd5
    	if (($stat[kills]>= 20 and $stat[ks] >= 1)
    					or
			($stat[kills]>= 30 and $stat[ks] >= 1)
    	    			or
			($stat[kills]>= 40)
        				or
        	($stat[gkills]>= 150)
	        			or
    	    ($stat[N]>= 30 and $stat[gkills] >= 40)
            			or
    	    ($stat[N]>= 20 and $stat[tankkils] / $stat[N] >= 6)
    	   )
    		$awd[5] = 1;
	    // awd6
    	if (($stat[kills]>= 40 and $stat[ks] >= 1)
    					or
			($stat[kills]>= 50 and $stat[ks] >= 0.6)
    	    			or
			($stat[kills]>= 60)
        				or
        	($stat[gkills]>= 300)
	        			or
        	($stat[tankkills]>= 150)
	        			or
    	    ($stat[N]>= 50 and $stat[gkills] >= 200)
    	   )
    		$awd[6] = 1;
	    // awd7
    	if (($stat[kills]>= 70 and $stat[ks] >= 1)
    					or
			($stat[kills]>= 80 and $stat[ks] >= 0.6)
    	    			or
			($stat[kills]>= 100)
	        			or
    	    ($stat[gkills]>= 600)
        				or
        	($stat[tankkills]>= 250)
	        			or
        	($stat[N]>= 150 and $stat[gkills] >= 300)
	       )
    		$awd[7] = 1;
    }
//    echo "$awd[1]:$awd[2]:$awd[3]:$awd[4]:$awd[5]:$awd[6]:$awd[7]:";
	return $awd;

}


?>